# delimit ;
clear ;
set more off ;
version 12.0 ;

cd "replication" ;

* ***************************************************************************** ;
* Carpena, Fenella, and Bilal Zia. "The causal mechanism of financial education: 
* Evidence from mediation analysis." Journal of Economic Behavior & Organization 
* 177 (2020): 143-184.
* 
* This do-file creates Figures 3-6 of the paper
* ***************************************************************************** ;

* ***************************************************************************** ;
* get endline data
* ***************************************************************************** ;

use "./data/endline.dta", clear ;

* merge with baseline ;
merge 1:1 id using "./data/baseline.dta" ;
tab _merge ;

* correction to the variable label ;
label var savings_bank "has bank savings account" ;

* ***************************************************************************** ;
* sample selection
* ***************************************************************************** ;

* drop households with baseline but no endline ;
drop if _merge == 2 ; 
drop _merge ;

* drop households with no financial knowledge scores ; 
egen temp_knowl = rowmean(numer_fin_return-attit_budget) ;
drop if temp_knowl == . ;
drop temp_knowl ; 

* identify respondents with endline measures of numeracy ;
merge 1:1 id using "./data/financial-knowledge-short-term", keepusing(id) ;
tab _merge ;
keep if _merge == 3 ;
drop _merge ;

* drop households with missing discount rate (control variable) ;
drop if disc_rate == . ;

* ***************************************************************************** ;
* create financial knowledge scores
* ***************************************************************************** ;

egen end_numeracy = rowmean(numer_fin_return-numer_int_rate) ;
egen end_awareness = rowmean(aware_budget-aware_unprod) ;
egen end_attitudes = rowmean(attit_suggest_ins-attit_budget) ;

* ***************************************************************************** ;
* create treatment dummies
* ***************************************************************************** ;

gen flcounsgoal = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 1)  ;
gen flcouns = (fin_ed_treatment == 1 & couns_treatment == 1 & goal_treatment == 0) ;
gen flgoal = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 1) ;
gen fl = (fin_ed_treatment == 1 & couns_treatment == 0 & goal_treatment == 0) ;
gen control = (fin_ed_treatment == 0 & couns_treatment == 0 & goal_treatment == 0) ;

* ***************************************************************************** ;
* pre-treatment control variables
* ***************************************************************************** ;

local cvars has_hard_time_saving interested_in_financial inconsistent disc_rate riskaverse  ;
local rcvars rhas_hard_time_saving rinterested_in_financial rinconsistent rdisc_rate rriskaverse  ;

* ***************************************************************************** ;
* rename variables to make them shorter ;
* ***************************************************************************** ;

ren budget_helpful bdg_help ;
ren budget_tried bdg_tried ;
ren budget_regular bdg_reg ;
ren savings_informal sav_inf ;
ren savings_bank sav_bnk ;
ren loan_purpose_bus_educ loan_bus ; 
ren ins_life ins_life ;

* ***************************************************************************** ;
* FIGURE 3: ACME, FL only 
* ***************************************************************************** ;

est drop _all ;

local t fl ;
local outcomes bdg_tried ; 

* remove strata FEs ;
foreach var of varlist `t' `outcomes' `cvars' end_awareness end_attitudes { ;
	qui reg `var' i.strata if `t' | control ;
	predict r`var', resid ;
	label var r`var' `var' ;
} ;

* loop over all outcomes and mediators ;
foreach y of local outcomes { ;
	foreach m of varlist end_awareness end_attitudes { ;
	
		* sensitivity analysis ; 
		di "*********** treatment: `t', outcome: `y', mediator: `m' ******************* " ;
		medsens		(regress r`m' r`t' `rcvars') 
					(regress r`y' r`t' r`m' `rcvars')
					if `t' | control,
					treat(r`t') mediate(r`m') sims(1000) ;

		* double check that the ACME in the table is the same as when rho == 0 ;
		di "***** double check that ACME in the table is the same as when rho == 0 *****" ;
		list _med_delta0 if _med_rho == 0 ;

		* rho for which the confidence interval contains zero ;
		di "***** rhos for which the 95% CI contains zero *****" ;
		list _med_updelta0 _med_lodelta0 _med_rho if _med_updelta0 > 0 & _med_lodelta0 < 0 ;

		* list all ACME and rhos ; 
		di "***** list of all rhos, ACMEs, and their CI *****" ;
		list _med_rho _med_delta0 _med_lodelta0 _med_updelta0  if _med_rho != .;

		* save graph ;
		twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
		line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

		* shorten m for the filename ; 
		local mshort = subinstr("`m'", "end_", "", .) ;
		
		graph export "./output/figure03-`t'-`y'-`mshort'.png", as(png) replace ;
		
		drop _* ;
	
	} ;
} ;

* ***************************************************************************** ;
* FIGURE 4: ACME, FL + goal
* ***************************************************************************** ;

est drop _all ;
drop rbdg_tried-rend_attitudes ;

local t flgoal ;
local outcomes bdg_tried sav_bnk ; 

* remove strata FEs ;
foreach var of varlist `t' `outcomes' `cvars' end_awareness end_attitudes { ;
	qui reg `var' i.strata if `t' | control ;
	predict r`var', resid ;
	label var r`var' `var' ;
} ;

* loop over all outcomes and mediators ;
foreach y of local outcomes { ;
	foreach m of varlist end_awareness end_attitudes { ;
	
		* sensitivity analysis ; 
		di "*********** treatment: `t', outcome: `y', mediator: `m' ******************* " ;
		medsens		(regress r`m' r`t' `rcvars') 
					(regress r`y' r`t' r`m' `rcvars')
					if `t' | control,
					treat(r`t') mediate(r`m') sims(1000) ;

		* double check that the ACME in the table is the same as when rho == 0 ;
		di "***** double check that ACME in the table is the same as when rho == 0 *****" ;
		list _med_delta0 if _med_rho == 0 ;

		* rho for which the confidence interval contains zero ;
		di "***** rhos for which the 95% CI contains zero *****" ;
		list _med_updelta0 _med_lodelta0 _med_rho if _med_updelta0 > 0 & _med_lodelta0 < 0 ;

		* list all ACME and rhos ; 
		di "***** list of all rhos, ACMEs, and their CI *****" ;
		list _med_rho _med_delta0 _med_lodelta0 _med_updelta0  if _med_rho != .;

		* save graph ;
		twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
		line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;
		
		* shorten m for the filename ; 
		local mshort = subinstr("`m'", "end_", "", .) ;
		
		graph export "./output/figure04-`t'-`y'-`mshort'.png", as(png) replace ;
		
		drop _* ;
	
	} ;
} ;

* ***************************************************************************** ;
* FIGURE 5: ACME, FL + couns
* ***************************************************************************** ;

est drop _all ;
drop rbdg_tried-rend_attitudes ;

local t flcouns ;
local outcomes bdg_tried sav_bnk loan_bus ins_life ; 

* remove strata FEs ;
foreach var of varlist `t' `outcomes' `cvars' end_awareness end_attitudes { ;
	qui reg `var' i.strata if `t' | control ;
	predict r`var', resid ;
	label var r`var' `var' ;
} ;

* loop over all outcomes and mediators ;
foreach y of local outcomes { ;
	foreach m of varlist end_awareness end_attitudes { ;
	
		* sensitivity analysis ; 
		di "*********** treatment: `t', outcome: `y', mediator: `m' ******************* " ;
		medsens		(regress r`m' r`t' `rcvars') 
					(regress r`y' r`t' r`m' `rcvars')
					if `t' | control,
					treat(r`t') mediate(r`m') sims(1000) ;

		* double check that the ACME in the table is the same as when rho == 0 ;
		di "***** double check that ACME in the table is the same as when rho == 0 *****" ;
		list _med_delta0 if _med_rho == 0 ;

		* rho for which the confidence interval contains zero ;
		di "***** rhos for which the 95% CI contains zero *****" ;
		list _med_updelta0 _med_lodelta0 _med_rho if _med_updelta0 > 0 & _med_lodelta0 < 0 ;

		* list all ACME and rhos ; 
		di "***** list of all rhos, ACMEs, and their CI *****" ;
		list _med_rho _med_delta0 _med_lodelta0 _med_updelta0  if _med_rho != .;

		* save graph ;
		twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
		line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

		* shorten m for the filename ; 
		local mshort = subinstr("`m'", "end_", "", .) ;
		
		graph export "./output/figure05-`t'-`y'-`mshort'.png", as(png) replace ;
		
		drop _* ;
	
	} ;
} ;

* ***************************************************************************** ;
* FIGURE 6: ACME, FL + couns + goal
* ***************************************************************************** ;

est drop _all ;
drop rbdg_tried-rend_attitudes ;

local t flcounsgoal ;
local outcomes bdg_tried sav_bnk ins_life ; 

* remove strata FEs ;
foreach var of varlist `t' `outcomes' `cvars' end_awareness end_attitudes { ;
	qui reg `var' i.strata if `t' | control ;
	predict r`var', resid ;
	label var r`var' `var' ;
} ;

* loop over all outcomes and mediators ;
foreach y of local outcomes { ;
	foreach m of varlist end_awareness end_attitudes { ;
	
		* sensitivity analysis ; 
		di "*********** treatment: `t', outcome: `y', mediator: `m' ******************* " ;
		medsens		(regress r`m' r`t' `rcvars') 
					(regress r`y' r`t' r`m' `rcvars')
					if `t' | control,
					treat(r`t') mediate(r`m') sims(1000) ;

		* double check that the ACME in the table is the same as when rho == 0 ;
		di "***** double check that ACME in the table is the same as when rho == 0 *****" ;
		list _med_delta0 if _med_rho == 0 ;

		* rho for which the confidence interval contains zero ;
		di "***** rhos for which the 95% CI contains zero *****" ;
		list _med_updelta0 _med_lodelta0 _med_rho if _med_updelta0 > 0 & _med_lodelta0 < 0 ;

		* list all ACME and rhos ; 
		di "***** list of all rhos, ACMEs, and their CI *****" ;
		list _med_rho _med_delta0 _med_lodelta0 _med_updelta0  if _med_rho != .;

		* save graph ;
		twoway rarea _med_updelta0 _med_lodelta0 _med_rho, bcolor(gs14) ||
		line _med_delta0 _med_rho, lcolor(black) lpattern(dash) ytitle("ACME") xtitle("Sensitivity parameter, {&rho}") legend(off) ;

		* shorten m for the filename ; 
		local mshort = subinstr("`m'", "end_", "", .) ;
		
		graph export "./output/figure06-`t'-`y'-`mshort'.png", as(png) replace ;
		
		drop _* ;
	
	} ;
} ;

exit ;
